'use strict';
const {getDefaultModelConfig} = require("../../utils/common");
const {formatDecimal} = require("../../utils");

module.exports = app => {
    const { STRING, INTEGER, DATE, TINYINT, TEXT, DECIMAL } = app.Sequelize;
    const Product = app.model.define(
        'Product',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            type: INTEGER,
            shop_user_id: INTEGER,
            product_category_id: {
                type: INTEGER,
                defaultValue: 0,
                set(val) {
                    if (!val) {
                        this.setDataValue('product_category_id', 0)
                    } else {
                        this.setDataValue('product_category_id', val)
                    }
                }
            },
            parent_id: {
                type: INTEGER,
                defaultValue: 0,
                set(val) {
                    if (!val) {
                        this.setDataValue('parent_id', 0)
                    }
                    else {
                        this.setDataValue('parent_id', val)
                    }
                }
            },
            ticket_shop_id: {
                type: INTEGER,
                defaultValue: 0,
                set(val) {
                    if (!val) {
                        this.setDataValue('ticket_shop_id', 0)
                    } else {
                        this.setDataValue('ticket_shop_id', val)
                    }
                }
            },
            title: STRING(255),
            unit: STRING(255),
            tags: TEXT,
            tags2: TEXT,
            dates: TEXT,
            description: STRING(255),
            content: TEXT,
            post_url: STRING(255),
            image_urls: TEXT,
            is_hot: {type: TINYINT, defaultValue: 0},
            is_recommend: {type: TINYINT, defaultValue: 0},
            virtual_sale_count: {type: INTEGER, defaultValue: 0},
            sale_count: {type: INTEGER, defaultValue: 0},
            // 返利金额
            rebate_price: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price'))
                },
            },
            // 平台收入
            rebate_price_platform: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_platform'))
                },
            },
            // 百分比
            rebate_price_platform_percent: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_platform_percent'))
                },
            },
            // 商家收入
            rebate_price_shop: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_shop'))
                },
            },
            // 分成比例
            rebate_price_shop_percent: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_shop_percent'))
                },
            },
            // 代理收入
            rebate_price_agent: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_agent'))
                },
            },
            // 分成比例
            rebate_price_agent_percent: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_agent_percent'))
                },
            },
            // 代理收入
            rebate_price_agent2: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_agent2'))
                },
            },
            // 分成比例
            rebate_price_agent_percent2: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('rebate_price_agent_percent2'))
                },
            },
            price: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('price'))
                },
            },
            original_price: {
                type: DECIMAL(10, 2), defaultValue: 0.00,
                get() {
                    return formatDecimal(this.getDataValue('original_price'))
                },
            },
            // 库存
            stock: {type: INTEGER, defaultValue: 0},
            sort: {type: INTEGER, defaultValue: 0},
            status: {type: TINYINT, defaultValue: 1},
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'product',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );
    Product.associate = function () {
        // 关联商家
        app.model.Business.Product.belongsTo(app.model.Business.TicketShop, {as: 'ticketShop', foreignKey: 'ticket_shop_id'});
        // 关联上级
        app.model.Business.Product.belongsTo(app.model.Business.Product, {as: 'parent', foreignKey: 'parent_id'});
        // 关联下级
        app.model.Business.Product.hasMany(app.model.Business.Product, {as: 'children', foreignKey: 'parent_id'});
    }
    return Product
};
